package cn.ht.jywrwater.module.ht.service.dataimport;

import cn.ht.jywrwater.module.ht.common.DeviceTypeEnum;
import cn.ht.jywrwater.module.ht.controller.admin.dataImport.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.customermeter.CustomerMeterDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.customermeterdevice.CustomerMeterDeviceDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.customers.CustomersDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.town.TownDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.village.VillageDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.villagemeter.VillageMeterDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.villagemeterdevice.VillageMeterDeviceDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterdevices.WaterDevicesDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualitydata.WaterQualityDataDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualitydevicefactor.WaterQualityDeviceFactorDO;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualityfactor.WaterQualityFactorDO;
import cn.ht.jywrwater.module.ht.dal.mysql.customermeter.CustomerMeterMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.customermeterdevice.CustomerMeterDeviceMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.customers.CustomersMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.town.TownMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.village.VillageMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.villagemeter.VillageMeterMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.villagemeterdevice.VillageMeterDeviceMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.waterdevices.WaterDevicesMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualitydata.WaterQualityDataMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualitydevicefactor.WaterQualityDeviceFactorMapper;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualityfactor.WaterQualityFactorMapper;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class DataImportServiceImpl implements DataImportService {

    @Resource
    private VillageMapper villageMapper;

    @Resource
    private VillageMeterDeviceMapper villageMeterDeviceMapper;

    @Resource
    private CustomerMeterDeviceMapper customerMeterDeviceMapper;

    @Resource
    private VillageMeterMapper villageMeterMapper;

    @Resource
    private TownMapper townMapper;

    @Resource
    private CustomersMapper customersMapper;

    @Resource
    private CustomerMeterMapper customerMeterMapper;

    @Resource
    private WaterQualityFactorMapper waterQualityFactorMapper;

    @Resource
    private WaterQualityDataMapper waterQualityDataMapper;

    @Resource
    private WaterQualityDeviceFactorMapper waterQualityDeviceFactorMapper;

    @Resource
    private WaterDevicesMapper waterDevicesMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importVillageDeviceDatas(List<VillageDeviceImportExcelVO> villageDeviceImportExcelVOList) {
        for (VillageDeviceImportExcelVO villageDeviceImportExcelVO : villageDeviceImportExcelVOList) {
            try {
                VillageDO villageDO = convertVdData2Village(villageDeviceImportExcelVO);
                VillageMeterDeviceDO waterMetersDO = convertVdData2Device(villageDeviceImportExcelVO);

                villageMapper.insert(villageDO);
                villageMeterDeviceMapper.insert(waterMetersDO);

                VillageMeterDO villageMeterDO = new VillageMeterDO();
                villageMeterDO.setMeterCode(waterMetersDO.getMeterCode());
                villageMeterDO.setVillageCode(villageDO.getVillageCode());
                villageMeterMapper.insert(villageMeterDO);
            } catch (Exception e) {
                log.error("villageDeviceImportExcelVO:{}", villageDeviceImportExcelVO);
                throw e;
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importCustomerDeviceDatas(List<CustomerDeviceImportExcelVO> CustomerDeviceImportExcelVOList) {
        for (CustomerDeviceImportExcelVO customerDeviceImportExcelVO : CustomerDeviceImportExcelVOList) {
            try {
                CustomersDO customersDO = convertVdData2Customer(customerDeviceImportExcelVO);
                CustomerMeterDeviceDO waterMetersDO = convertCdData2Device(customerDeviceImportExcelVO);

                customersMapper.insert(customersDO);
                customerMeterDeviceMapper.insert(waterMetersDO);

                CustomerMeterDO customerMeterDO = new CustomerMeterDO();
                customerMeterDO.setCustomerCode(customersDO.getCustomerCode());
                customerMeterDO.setMeterCode(waterMetersDO.getMeterCode());
                customerMeterMapper.insert(customerMeterDO);
            } catch (Exception e) {
                log.error("customerDeviceImportExcelVO:{}", customerDeviceImportExcelVO);
                throw e;
            }
        }

    }

    @Override
    public void importTowns(List<TownImportExcelVO> TownImportExcelVOList) {
        for (TownImportExcelVO townImportExcelVO : TownImportExcelVOList) {
            TownDO townDo = new TownDO();
            townDo.setTownCode(townImportExcelVO.getTownCode());
            townDo.setAddress(townImportExcelVO.getAddress());
            townDo.setArea(townImportExcelVO.getArea());
            townDo.setHouseholdPerson(townImportExcelVO.getHouseholdPerson());
            townDo.setTownName(townImportExcelVO.getTownName());
            townMapper.insert(townDo);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importWaterQualityFactor(List<WaterQualityFactorImportExcelVO> waterQualityFactorImportExcelVOList) {
        for (WaterQualityFactorImportExcelVO waterQualityFactorImportExcelVO : waterQualityFactorImportExcelVOList) {

            String[] split1 = waterQualityFactorImportExcelVO.getCreateTimeString().split(" ");

            String[] split = split1[0].split("/");

            String[] split2 = split1[1].split(":");


            LocalDateTime localDateTime = LocalDateTimeUtil.now();
            LocalDateTime localDateTime1 = localDateTime.withYear(Integer.parseInt(split[2]))
                    .withMonth(Integer.parseInt(split[0])).withDayOfMonth(Integer.parseInt(split[1]))
                    .withHour(Integer.parseInt(split2[0])).withMinute(Integer.parseInt(split2[1]))
                    .withSecond(Integer.parseInt(split2[2]));
            waterQualityFactorImportExcelVO.setCreateTime(localDateTime1);

            WaterQualityFactorDO bean = BeanUtil.toBean(waterQualityFactorImportExcelVO, WaterQualityFactorDO.class);
            waterQualityFactorMapper.insert(bean);
        }

    }

    @Override
    public void importWaterQualityData(List<WaterQualityDataImportExcelVO> waterQualityDataImportExcelVOList) {
        for (WaterQualityDataImportExcelVO waterQualityDataImportExcelVO : waterQualityDataImportExcelVOList) {
            WaterQualityDataDO bean = new WaterQualityDataDO();
            bean.setQualityType(waterQualityDataImportExcelVO.getQualityType());
            bean.setDeviceValue(waterQualityDataImportExcelVO.getDeviceValue());
            bean.setDeviceCode(waterQualityDataImportExcelVO.getDeviceCode());



            /////
            String[] split1 = waterQualityDataImportExcelVO.getCreateTimeString().split(" ");

            String[] split = split1[0].split("/");

            String[] split2 = split1[1].split(":");

            LocalDateTime localDateTime = LocalDateTimeUtil.now();
            LocalDateTime localDateTime1 = localDateTime.withYear(Integer.parseInt(split[2]))
                    .withMonth(Integer.parseInt(split[0])).withDayOfMonth(Integer.parseInt(split[1]))
                    .withHour(Integer.parseInt(split2[0])).withMinute(Integer.parseInt(split2[1]))
                    .withSecond(Integer.parseInt(split2[2]));
            bean.setCreateTime(localDateTime1);


            String[] dataTimeSplit1 = waterQualityDataImportExcelVO.getDataTimeString().split(" ");

            String[] sdataTimeSplit = dataTimeSplit1[0].split("/");

            String[] dataTimeSplit2 = dataTimeSplit1[1].split(":");

            LocalDateTime localDateTimeAA = LocalDateTimeUtil.now();
            LocalDateTime localDateTimeBB = localDateTimeAA.withYear(Integer.parseInt(sdataTimeSplit[2]))
                    .withMonth(Integer.parseInt(sdataTimeSplit[0])).withDayOfMonth(Integer.parseInt(sdataTimeSplit[1]))
                    .withHour(Integer.parseInt(dataTimeSplit2[0])).withMinute(Integer.parseInt(dataTimeSplit2[1]))
                    .withSecond(Integer.parseInt(dataTimeSplit2[2]));
            bean.setDataTime(localDateTimeBB);
            //////
            waterQualityDataMapper.insert(bean);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importWaterQualityDevice(List<WaterQualityDeviceImportExcelVO> waterQualityDeviceImportExcelVOList) {
        for (WaterQualityDeviceImportExcelVO waterQualityDeviceImportExcelVO : waterQualityDeviceImportExcelVOList) {
            WaterDevicesDO waterDevicesDO = new WaterDevicesDO();
            waterDevicesDO.setDeviceCode(waterQualityDeviceImportExcelVO.getDeviceCode());
            waterDevicesDO.setAddress(waterQualityDeviceImportExcelVO.getDeviceName());
            waterDevicesDO.setLat(ObjectUtil.isNull(waterQualityDeviceImportExcelVO.getLat()) ? null :
                    NumberUtil.round(waterQualityDeviceImportExcelVO.getLat(), 6));
            waterDevicesDO.setLng(ObjectUtil.isNull(waterQualityDeviceImportExcelVO.getLng()) ? null :
                    NumberUtil.round(waterQualityDeviceImportExcelVO.getLng(), 6));
            if (waterQualityDeviceImportExcelVO.getDeviceType() == 1) {
                waterDevicesDO.setDeviceType(DeviceTypeEnum.CCS.getType());
            }else {
                //2
                waterDevicesDO.setDeviceType(DeviceTypeEnum.MSS.getType());
            }
            String waterQualityFactors = waterQualityDeviceImportExcelVO.getWaterQualityFactors();
            waterDevicesMapper.insert(waterDevicesDO);
            List<String> split = StrUtil.split(waterQualityFactors, ",");
            for (String s : split) {
                WaterQualityDeviceFactorDO waterQualityDeviceFactorDO = new WaterQualityDeviceFactorDO();
                waterQualityDeviceFactorDO.setWaterQualityDeviceCode(waterDevicesDO.getDeviceCode());
                waterQualityDeviceFactorDO.setWaterQualityFactorCode(s);
                waterQualityDeviceFactorMapper.insert(waterQualityDeviceFactorDO);
            }

        }

    }


    private CustomersDO convertVdData2Customer(CustomerDeviceImportExcelVO customerDeviceImportExcelVO) {
        CustomersDO customersDO = new CustomersDO();
        customersDO.setCustomerCode(customerDeviceImportExcelVO.getCustomerCode());
        customersDO.setCustomerName(customerDeviceImportExcelVO.getCustomerName());
//        customersDO.setAddress(customerDeviceImportExcelVO.getAddress());
        customersDO.setCustomerTel(customerDeviceImportExcelVO.getCustomerTel());
        customersDO.setVillage(customerDeviceImportExcelVO.getVillage());
        customersDO.setLat(ObjectUtil.isNull(customerDeviceImportExcelVO.getLat()) ? null :
                NumberUtil.round(customerDeviceImportExcelVO.getLat(), 6));
        customersDO.setLng(ObjectUtil.isNull(customerDeviceImportExcelVO.getLng()) ? null :
                NumberUtil.round(customerDeviceImportExcelVO.getLng(), 6));
        customersDO.setContactor(customerDeviceImportExcelVO.getContactor());
        customersDO.setPersonCount(ObjectUtil.isNull(customerDeviceImportExcelVO.getHouseholdPerson()) ? null :
                Integer.parseInt(customerDeviceImportExcelVO.getHouseholdPerson()));
        return customersDO;
    }

    private CustomerMeterDeviceDO convertCdData2Device(CustomerDeviceImportExcelVO customerDeviceImportExcelVO) {

        CustomerMeterDeviceDO waterMetersDO = new CustomerMeterDeviceDO();
        waterMetersDO.setMeterCode(customerDeviceImportExcelVO.getDeviceCode());
        waterMetersDO.setAddress(customerDeviceImportExcelVO.getAddress());
        waterMetersDO.setBelongTo(customerDeviceImportExcelVO.getVillage());
        waterMetersDO.setVillage(customerDeviceImportExcelVO.getVillage());


        waterMetersDO.setLat(ObjectUtil.isNull(customerDeviceImportExcelVO.getLat()) ? null :
                NumberUtil.round(customerDeviceImportExcelVO.getLat(), 6));
        waterMetersDO.setLng(ObjectUtil.isNull(customerDeviceImportExcelVO.getLng()) ? null :
                NumberUtil.round(customerDeviceImportExcelVO.getLng(), 6));
        waterMetersDO.setContractor(customerDeviceImportExcelVO.getConstructor());
        waterMetersDO.setCustomerTel(customerDeviceImportExcelVO.getCustomerTel());
        String meterConstructTime = customerDeviceImportExcelVO.getMeterConstructTime();
        if (StrUtil.isNotEmpty(meterConstructTime)) {
            String[] split = meterConstructTime.split("/");

            LocalDateTime localDateTime = LocalDateTimeUtil.now();
            localDateTime.withYear(Integer.parseInt(split[2]))
                    .withMonth(Integer.parseInt(split[0])).withDayOfMonth(Integer.parseInt(split[1]));
            waterMetersDO.setMeterTime(localDateTime);
        }
        waterMetersDO.setOriginVolume(customerDeviceImportExcelVO.getMeterInitValue());
        waterMetersDO.setMaxVolume(ObjectUtil.isNull(customerDeviceImportExcelVO.getMaxMeterValue()) ? null :
                Double.parseDouble(customerDeviceImportExcelVO.getMaxMeterValue()));

        return waterMetersDO;
    }

    private VillageDO convertVdData2Village(VillageDeviceImportExcelVO villageDeviceImportExcelVO) {
        VillageDO villageDO = new VillageDO();
        villageDO.setVillageCode(villageDeviceImportExcelVO.getVillageCode());
        villageDO.setVillageName(villageDeviceImportExcelVO.getVillageName());
        villageDO.setTownCode(villageDeviceImportExcelVO.getTownCode());
        villageDO.setWaterStation(villageDeviceImportExcelVO.getWaterStation());
        villageDO.setLat(ObjectUtil.isNull(villageDeviceImportExcelVO.getLat()) ? null :
                BigDecimal.valueOf(Double.parseDouble(villageDeviceImportExcelVO.getLat())));
        villageDO.setLng(ObjectUtil.isNull(villageDeviceImportExcelVO.getLng()) ? null :
                BigDecimal.valueOf(Double.parseDouble(villageDeviceImportExcelVO.getLng())));
        villageDO.setPersonCharge(villageDeviceImportExcelVO.getPersonCharge());
        villageDO.setCustomerTel(villageDeviceImportExcelVO.getCustomerTel());
        villageDO.setHousehold(ObjectUtil.isNull(villageDeviceImportExcelVO.getHousehold()) ? null :
                Integer.parseInt(villageDeviceImportExcelVO.getHousehold()));
        return villageDO;
    }

    private VillageMeterDeviceDO convertVdData2Device(VillageDeviceImportExcelVO villageDeviceImportExcelVO) {

        VillageMeterDeviceDO waterMetersDO = new VillageMeterDeviceDO();
        waterMetersDO.setMeterCode(villageDeviceImportExcelVO.getMeterNo());
        waterMetersDO.setBelongTo(villageDeviceImportExcelVO.getVillageCode());
        waterMetersDO.setVillage(villageDeviceImportExcelVO.getVillageCode());
        waterMetersDO.setWaterStation(villageDeviceImportExcelVO.getWaterStation());
        waterMetersDO.setLat(ObjectUtil.isNull(villageDeviceImportExcelVO.getLat()) ? null :
                BigDecimal.valueOf(Double.parseDouble(villageDeviceImportExcelVO.getLat())));
        waterMetersDO.setLng(ObjectUtil.isNull(villageDeviceImportExcelVO.getLng()) ? null :
                BigDecimal.valueOf(Double.parseDouble(villageDeviceImportExcelVO.getLng())));
        waterMetersDO.setContractor(villageDeviceImportExcelVO.getConstructor());
        waterMetersDO.setCustomerTel(villageDeviceImportExcelVO.getCustomerTel());
        String meterConstructTime = villageDeviceImportExcelVO.getMeterConstructTime();
//        waterMetersDO.setMeterTime(meterConstructTime)
        waterMetersDO.setOriginVolume(villageDeviceImportExcelVO.getMeterInitValue());
        waterMetersDO.setMaxVolume(ObjectUtil.isNull(villageDeviceImportExcelVO.getMaxMeterValue()) ? null :
                Double.parseDouble(villageDeviceImportExcelVO.getMaxMeterValue()));
        waterMetersDO.setCalibre(ObjectUtil.isNull(villageDeviceImportExcelVO.getCalibre()) ? null :
                Integer.parseInt(villageDeviceImportExcelVO.getCalibre()));

        return waterMetersDO;
    }

}
